package com.cntv.viewmodeldemo.savestate

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModel
import com.cntv.viewmodeldemo.databinding.ActivityKtxBinding
import com.cntv.viewmodeldemo.R


/**
 * Created By Mahongyin
 * Date    2020/9/17 16:48
 *使用 fragment-ktx扩展 创建viewmodel实例
 * 使用了databindinng  livedata viewmodel+savastate
 */
class KTXActivity : AppCompatActivity() {
    private lateinit var binding: ActivityKtxBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView<ActivityKtxBinding>(this, R.layout.activity_ktx)
//      var  myViewModel = ViewModelProvider(this,SavedStateViewModelFactory(application,this))[MyLiveDataViewModel::class.java]
       //通过fragment-ktx提供的新方法 更简单实例化viewmodel
        val myViewModel: KTXSaveStateViewModel by viewModels()

        binding.data = myViewModel
        binding.lifecycleOwner = this
    }
}